#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sqlite3 as db

THIS_DB_PATH = "pi.db"

def selectSqllite(sql):
    conn = db.connect(THIS_DB_PATH)
    cursor = conn.cursor()
    conn.row_factory = db.Row
    cursor.execute(sql)
    rows = cursor.fetchall()
    conn.close()
    return rows

def selectOneSqllite(sql):
    conn = db.connect(THIS_DB_PATH)
    cursor = conn.cursor()
    conn.row_factory = db.Row
    cursor.execute(sql)
    rows = cursor.fetchall()
    conn.close()
    if len(rows):
        return rows[0]
    else:
        return 0


def createSqllite(sql):
    conn = db.connect(THIS_DB_PATH)
    cursor = conn.cursor()
    cursor.execute(sql)
    rowId = cursor.lastrowid
    conn.commit()
    conn.close()
    return rowId

def updateSqllite(sql):
    conn = db.connect(THIS_DB_PATH)
    cursor = conn.cursor()
    cursor.execute(sql)
    conn.commit()
    conn.close()

def deleteSqllite(sql):
    conn = db.connect(THIS_DB_PATH)
    cursor = conn.cursor()
    cursor.execute(sql)
    conn.commit()
    conn.close()